from loguru import logger
import requests
from faker import Faker


def outer(func):
    def inner(*args, **kwargs):
        logger.info(f"执行函数名称: {func.__name__}")
        return func(*args, **kwargs)

    return inner


def login():
    headers = {'content-type': 'application/json'}
    json_data = {'account': 'demo', 'pwd': 'crmeb.com', 'captchaVO': {}, }
    url = 'https://api.admin.merchant.java.crmeb.net/api/admin/platform/login'
    response = requests.post(url, headers=headers, json=json_data, )
    return response.json()


class BaseRequest:  # 请求
    def __init__(self):
        self.token = login()['data']['token']
        self.url = 'https://api.admin.merchant.java.crmeb.net/api/admin/platform/circle'
        self.faker = BaseFaker()
        self.session = requests.Session()
        self.session.headers.update({'authori-zation': self.token, 'content-type': 'application/json'})

    def get(self, url):
        logger.debug(f'接口请求： {url}')
        response = self.session.get(url, params=None)
        logger.debug(f'接口响应： {response.text}')
        return response.json()

    def post(self, url, json_data=None):
        logger.debug(f'接口请求： {url}')
        response = self.session.post(url, json=json_data)
        logger.debug(f'接口响应： {response.text}')
        return response.json()


class BaseFaker:  # 造假数据
    def __init__(self):
        self.fake = Faker("zh_CN")

    @outer
    def Create_Data(self):
        return {
            'account': self.fake.name() + '(圈层)',
            'address': '',
            'latitude': '',
            'longitude': '',
            'name': self.fake.name() + '(name)',
            'phone': self.fake.phone_number(),
            'range': '',
            'realName': self.fake.name() + '(realName)',
            'role': 1,
            'addressSwitch': 2,
            'pwd': self.fake.password(),
        }
